package com.qf.cx.simplenms.webservice.model;

import java.util.ArrayList;
import java.util.List;

import com.qf.cx.simplenms.persist.entity.ReaderGroupEntity;
import com.qf.cx.simplenms.persist.entity.SubscriberEntity;
import com.qf.cx.simplenms.persist.entity.TagMemoryEntity;
import com.qf.cx.simplenms.persist.entity.TopicEntity;

public class Topic {
	private String name;
	private long cycle;
	private List<String> readerGroupNames = new ArrayList<>();
	private List<String> tagMemoryNames = new ArrayList<>();
	private List<Long> subscriberIds = new ArrayList<>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getCycle() {
		return cycle;
	}

	public void setCycle(long cycle) {
		this.cycle = cycle;
	}

	public List<String> getReaderGroupNames() {
		return readerGroupNames;
	}

	public void setReaderGroupNames(List<String> readerGroupNames) {
		this.readerGroupNames = readerGroupNames;
	}

	public List<String> getTagMemoryNames() {
		return tagMemoryNames;
	}

	public void setTagMemoryNames(List<String> tagMemoryNames) {
		this.tagMemoryNames = tagMemoryNames;
	}

	public List<Long> getSubscriberIds() {
		return subscriberIds;
	}

	public void setSubscriberIds(List<Long> subscriberIds) {
		this.subscriberIds = subscriberIds;
	}

	public static Topic from(TopicEntity topic) {
		Topic o = new Topic();
		o.cycle = topic.getCycle();
		o.name = topic.getName();
		List<String> readerGroupNames = new ArrayList<>();
		topic.getReaderGroupEntities().forEach(t -> readerGroupNames.add(t.getName()));
		o.setReaderGroupNames(readerGroupNames);

		List<Long> subscriberIds = new ArrayList<>();
		topic.getSubscriberEntities().forEach(t -> subscriberIds.add(t.getId()));
		o.setSubscriberIds(subscriberIds);

		List<String> tagMemoryNames = new ArrayList<>();
		topic.getTagMemoryEntities().forEach(t -> tagMemoryNames.add(t.getName()));
		o.setTagMemoryNames(tagMemoryNames);
		return o;
	}

	public TopicEntity toTopicEntity() {
		TopicEntity o = new TopicEntity();
		o.setName(name);
		o.setCycle(cycle);

		List<ReaderGroupEntity> readerGroupEntities = new ArrayList<>();
		readerGroupNames.forEach(t -> {
			ReaderGroupEntity e = new ReaderGroupEntity();
			e.setName(t);
			readerGroupEntities.add(e);
		});
		o.setReaderGroupEntities(readerGroupEntities);

		List<SubscriberEntity> subscriberEntities = new ArrayList<>();
		subscriberIds.forEach(t -> {
			SubscriberEntity e = new SubscriberEntity();
			e.setId(t);
			subscriberEntities.add(e);
		});
		o.setSubscriberEntities(subscriberEntities);

		List<TagMemoryEntity> tagMemoryEntities = new ArrayList<>();
		tagMemoryNames.forEach(t -> {
			TagMemoryEntity e = new TagMemoryEntity();
			e.setName(t);
			tagMemoryEntities.add(e);
		});
		o.setTagMemoryEntities(tagMemoryEntities);
		return o;
	}

	@Override
	public String toString() {
		return "Topic [name=" + name + ", cycle=" + cycle + ", readerGroupNames=" + readerGroupNames
				+ ", tagMemoryNames=" + tagMemoryNames + ", subscriberIds=" + subscriberIds + "]";
	}
}
